﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DTBuilderLib.BinaryIO
{
    /// <summary>
    /// Read and write the items.bin binary file.
    /// </summary>
    public class ItemsFileIO
    {
        public static Collection Load(FileInfo file)
        {
            using (BinaryReader br = new BinaryReader(file.OpenRead()))
            {
                Collection result = new Collection();

                result.Version = (int) br.ReadByte();
                result.UseStringsFile = br.ReadInt32();
                result.MinLevel = br.ReadInt32();
                result.MaxLevel = br.ReadInt32();
                result.PageSizeLog2 = br.ReadInt32();
                result.PageQuality = br.ReadInt32();

                byte[] pageformat = br.ReadBytes(8);
                for (int i = 0; i < 8; i++)
                {
                    if (i >= pageformat.Length)
                    {
                        result.PageFormat += '\0';
                    }
                    else
                    {
                        result.PageFormat += (char)pageformat[i];
                    }
                }

                result.NextItemId = br.ReadInt32();
                result.ItemCount = br.ReadInt32();

                List<CollectionThumbnailsThumbnail> thumbs = new List<CollectionThumbnailsThumbnail>();
                for (int i = 0; i < result.ItemCount; i++)
                {
                    CollectionThumbnailsThumbnail thumb = new CollectionThumbnailsThumbnail();
                    thumb.Id = br.ReadInt32();
                    thumb.MinLevel = br.ReadInt32();
                    thumb.MaxLevel = br.ReadInt32();
                    thumb.SizeX = br.ReadSingle();
                    thumb.SizeY = br.ReadSingle();
                    thumbs.Add(thumb);
                }
                List<CollectionItemsItem> items = new List<CollectionItemsItem>();
                for (int i = 0; i < result.ItemCount; i++)
                {
                    CollectionItemsItem item = new CollectionItemsItem();
                    item.Id = br.ReadInt32();
                    item.Init = BinaryHelpers.ReadString(br);
                    item.Relative = br.ReadByte();
                    item.Type = BinaryHelpers.ReadString(br);
                    item.Thumb = br.ReadInt32();
                    items.Add(item);
                }

                result.Thumbnails = new CollectionThumbnails[1];
                result.Items = new CollectionItems[1];

                result.Thumbnails[0] = new CollectionThumbnails();
                result.Items[0] = new CollectionItems();

                result.Thumbnails[0].Thumbnail = thumbs.ToArray();
                result.Items[0].Item = items.ToArray();

                br.Close();
                return result;
            }
        }

        /// <summary>
        /// Write out the Collection to a file.
        /// </summary>
        /// <param name="c"></param>
        /// <param name="newguy"></param>
        public static void Save(Collection c, FileInfo filespec)
        {
            using (BinaryWriter wr = new BinaryWriter(filespec.Create()))
            {
                wr.Write((byte)c.Version);
                wr.Write((int)c.UseStringsFile);
                wr.Write((int)c.MinLevel);
                wr.Write((int)c.MaxLevel);
                wr.Write((int)c.PageSizeLog2);
                wr.Write((int)c.PageQuality);

                byte[] format = new byte[8];
                int slen = c.PageFormat.Length;
                if (slen > 8)
                {
                    slen = 8;
                }
                for (int i = 0; i < 8; i++)
                {
                    if (i < slen)
                    {
                        format[i] = (byte) c.PageFormat[i];
                    }
                    else
                    {
                        format[i] = (byte) '\0';
                    }
                }
                wr.Write(format);

                wr.Write((int)c.NextItemId);
                wr.Write((int)c.ItemCount);

                if (c.Thumbnails != null
                    && c.Thumbnails.Length >= 1
                    && c.Thumbnails[0] != null
                    && c.Thumbnails[0].Thumbnail != null)
                {
                    foreach (var thum in c.Thumbnails[0].Thumbnail)
                    {
                        wr.Write((int)thum.Id);
                        wr.Write((int)thum.MinLevel);
                        wr.Write((int)thum.MaxLevel);
                        wr.Write((float)thum.SizeX);
                        wr.Write((float)thum.SizeY);
                    }
                }

                if (c.Items != null
                    && c.Items.Length >= 1
                    && c.Items[0] != null
                    && c.Items[0].Item != null)
                {
                    foreach (var item in c.Items[0].Item)
                    {
                        wr.Write((int)item.Id);
                        BinaryHelpers.WriteString(wr, item.Init);
                        wr.Write((byte)item.Relative);
                        BinaryHelpers.WriteString(wr, item.Type);
                        wr.Write((int)item.Thumb);
                    }
                }
                
                wr.Close();
            }
        }
    }
}
